const app = getApp()
Page({
  data: {
    movie: {},
    loading: true,
    // reviews 需要单独拿出
    reviews: [],
    // 剧照预览时用
    photoList: [],
    page: 1,
    size: 20,
    // 控制电影简介展开
    showSummary: false
  },
  // 跳转到影评信息页
  bindNavigateTo: function (e) {
    let targetData = JSON.stringify(e.currentTarget.dataset.review)
    wx.navigateTo({
      url: '../review/review?review=' + encodeURIComponent(targetData)
    })
  },
  // 显示更多简介
  showSummary: function (e) {
    this.setData({
      showSummary: true
    })
  },
  // 图片预览
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.dataset.src,
      urls: this.data.photoList
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this
    wx.setNavigationBarTitle({
      title: options.title,
    })
    // 获取电影条目
    app.douban.findOne(options.id, function (res) {
      _this.setData({
        movie: res.data,
        loading: false
      })
      for (let i = 0, length = _this.data.movie.photos.length; i < length; i++) {
        _this.data.photoList.push(_this.data.movie.photos[i].image)
      }
      _this.setData({
        photoList: _this.data.photoList
      })
      // 影评单独获取，只取4条
      app.douban.findComments('reviews', _this.data.movie.id, 1, 4, function (res) {
        _this.setData({
          reviews: res.data.reviews
        })
      })
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.movie.title,
      path: `pages/item/item?id=${this.data.movie.id}&title=${this.data.movie.title}`
    }
  }
})